<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-05 12:05:33
 * @LastEditTime: 2023-10-20 09:28:33
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Storage;

use App\Http\Services\Service;
use App\Repositories\AttachmentRepository;
use App\Repositories\MpcInventoryDetailRepository;
use App\Repositories\MpcInventoryRepository;
use Illuminate\Support\Facades\DB;

/**
 * @property MpcInventoryRepository $repository
 * @property MpcInventoryDetailRepository $Detail
 * @property AttachmentRepository $att
 */

class InventoryService extends Service
    {


    public function boot(MpcInventoryRepository $Repository, MpcInventoryDetailRepository $detail, AttachmentRepository $att)
        {
        $this->repository = $Repository;
        $this->Detail     = $detail;
        $this->att        = $att;
        }
    public function list()
        {
        $data = $this->repository->condition(request())->getGrid();
        return $data;
        }
    public function info($requset)
        {
        return $this->repository->condition($requset)->getOne();
        }

    public function details()
        {
        return $this->Detail->condition(request())->getGrid();
        }
    protected function save($request)
        {
        $ReqData = $request->toArray();
        // DB::enableQueryLog();
        // dd(DB::getQueryLog());

        $data = $this->repository->whereId($request->input("id"))->first();
        if ($data && $data->status_id > 0) {
            fail("已审核单据禁止编辑！");
            }
        $ReqData['fect_auxiliary_quantity'] =
            $ReqData['storage_auxiliary_quantity'] =
            $ReqData['fect_quantity'] = $ReqData['storage_quantity'] =
            $ReqData['quantity'] =
            $ReqData['auxiliary_quantity'] = 0;

        $ReqData['details'] = collect($ReqData['details'])->map(function ($row) use (&$ReqData)
            {
            $row['fect_quantity']           = is_numeric($row['fect_quantity']) ? $row['fect_quantity'] : 0;
            $row['fect_auxiliary_quantity'] = is_numeric($row['fect_auxiliary_quantity']) ? $row['fect_auxiliary_quantity'] : 0;

            $row['quantity']           = $row['fect_quantity'] - $row['storage_quantity'];
            $row['auxiliary_quantity'] = $row['fect_auxiliary_quantity'] - $row['storage_auxiliary_quantity'];

            $ReqData['fect_auxiliary_quantity'] += $row['fect_auxiliary_quantity'];
            $ReqData['storage_auxiliary_quantity'] += $row['storage_auxiliary_quantity'];

            $ReqData['fect_quantity'] += $row['fect_quantity'];
            $ReqData['storage_quantity'] += $row['storage_quantity'];

            $ReqData['quantity'] += $row['quantity'];
            $ReqData['auxiliary_quantity'] += $row['auxiliary_quantity'];
            return $row;
            });

        $gdata = $this->repository->autosave($ReqData);
        if ($request->need_audit) {
            $this->audit([$gdata->id]);
            }
        return $gdata;
        }
    protected function audit($ids = null)
        {
        return $this->repository
            ->whereIn("id", $ids ?? $this->getRequestIds())->with(['details'])
            ->get()
            ->filter(function ($v)
                {
                if ($v->status_id != 0)
                    fail("单据:[{$v->code}]已审核！");
                auditSave($v);
                $v->details->filter(
                    function ($deltail) use ($v)
                        {
                        if ($deltail->quantity < 0) {
                            $deltail->useStorage("盘点", $v->code, 0 - $deltail->quantity)->subStorage();
                            }
                        else
                            $deltail->addStorage("盘点", $v->code);
                        }
                );
                });
        }
    protected function faudit()
        {
        $ids = $this->Detail->whereIn("id", $this->getRequestIds())->pluck("main_id");
        return $this->repository
            ->whereIn("id", $ids)
            ->get()
            ->filter(function ($v)
                {
                if ($v->status_id != 0)
                    fail("单据:[{$v->code}][{$v->status->name}]！审核失败");
                $v->financeremark = request("financeremark");
                $v->details->filter(
                    function ($deltail) use ($v)
                        {
                        $deltail->addStorage("盘点", $v->code);
                        }
                );
                auditSave($v);
                });
        }

    protected function nofaudit()
        {
        $ids = $this->Detail->whereIn("id", $this->getRequestIds())->pluck("main_id");
        return $this->repository
            ->whereIn("id", $ids)
            ->get()
            ->filter(function ($v)
                {
                if ($v->status_id != 1)
                    fail("单据:[{$v->code}][{$v->status->name}]！反审核失败");
                $v->details->filter(
                    function ($deltail) use ($v)
                        {
                        // if ($deltail->quantity < 0) {
                        //     fail("盘亏需要财务部门在财务页面反审核！");
                        //     }
                        $deltail->noaddStorage();
                        }
                );
                auditSave($v, false);
                });
        }
    protected function noaudit($ids = null)
        {
        return $this->repository
            ->whereIn("id", $ids ?? $this->getRequestIds())->with(['details'])
            ->get()
            ->filter(function ($v)
                {
                if ($v->status_id != 1)
                    fail("单据:[{$v->code}]未审核！");
                auditSave($v, false);
                $v->details->filter(
                    function ($deltail) use ($v)
                        {
                        if ($deltail->quantity < 0) {
                            $deltail->nosubStorage()->nouseStorage();
                            }
                        else
                            $deltail->noaddStorage();
                        }
                );

                $v->save();
                });
        }

    protected function delete($ids = null)
        {
        return $this->repository
            ->whereIn("id", $ids ?? $this->getRequestIds())
            ->get()
            ->filter(function ($v)
                {
                if ($v->status_id != 0)
                    fail("单据:[{$v->code}]已审核！无法删除");
                $v->details->filter(
                    function ($detail)
                        {
                        $detail->delete();
                        }
                );
                $v->delete();
                });
        }
    protected function auditItem()
        {
        $ids = $this->Detail->whereIn("id", $this->getRequestIds())->pluck("main_id");
        return $this->audit($ids);
        }
    protected function noauditItem()
        {
        $ids = $this->Detail->whereIn("id", $this->getRequestIds())->pluck("main_id");
        return $this->noaudit($ids);
        }
    protected function deleteItem()
        {
        $ids = $this->Detail->whereIn("id", $this->getRequestIds())->pluck("main_id");
        return $this->delete($ids);
        }
    }